﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace SherlockGUI.CFG
{
    class IniFile
    {
        public string m_strIniPath;
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);

        public IniFile(string strIniPath)
        {
            m_strIniPath = strIniPath;
        }
        /// <summary> 
        /// 写入ini文件 
        /// </summary> 
        /// <param name="Section">Section</param> 
        /// <param name="Key">键</param> 
        /// <param name="Value">值</param> 
        public void writeIni(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.m_strIniPath);
        }

        /// <summary> 
        /// 写入ini文件，不管section，默认放在default里 
        /// </summary> 
        /// <param name="Key">键</param> 
        /// <param name="Value">值</param> 
        public void writeIni(string Key, string Value)
        {
            WritePrivateProfileString("default", Key, Value, this.m_strIniPath);
        }

        /// <summary> 
        /// 读取ini文件 
        /// </summary> 
        /// <param name="Section">Section</param> 
        /// <param name="Key">键</param> 
        /// <returns>返回的值</returns> 
        public string readIni(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(256);
            int i = GetPrivateProfileString(Section, Key, "", temp, 256, this.m_strIniPath);
            return temp.ToString();
        }
        public string readString(string Section, string Key, string strDefault)
        {
            StringBuilder temp = new StringBuilder(256);
            int i = GetPrivateProfileString(Section, Key, strDefault, temp, 256, this.m_strIniPath);
            return temp.ToString();
        }

        public void WriteString(string section, string key, string strVal)
        {
            WritePrivateProfileString(section, key, strVal, m_strIniPath);
        }

        /// <summary> 
        /// 读取section，不管section，默认从default里读取 
        /// </summary> 
        /// <param name="Key">键</param> 
        /// <returns>返回值</returns> 
        public string readIni(string Key)
        {
            return readIni("default", Key);
        }

        /// <summary>
        /// 写入INI文件(int)
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        /// <param name="Value">值</param>
        public void WriteInt(string section, string key, int iVal)
        {
            WritePrivateProfileString(section, key, iVal.ToString(), m_strIniPath);
        }
        /// <summary>
        /// 读出INI文件(int)
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        public int ReadInt(string section, string key, int def)
        {
            return GetPrivateProfileInt(section, key, def, m_strIniPath);
        }

        /// <summary>
        /// 读出INI文件(int)
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        public bool ReadBool(string section, string key, bool def)
        {
            int iTmp = 0;
            if (def)
            {
                iTmp = 1;
            }

            int iRet = GetPrivateProfileInt(section, key, iTmp, m_strIniPath);
            if (iRet != 0)
            {
                return true;
            }

            return false;
        }

        public double readDouble(string section, string key,double dDef)
        {
            string strRet = readIni(section, key);
            double dRet = dDef;

            
            if (strRet.Length > 0)
            {
                try
                {
                    dRet = System.Convert.ToDouble(strRet);
                }
                catch
                {
                    dRet = dDef;
                }
            }

            return dRet;
        }

        public void WriteBool(string section, string key, bool bVal)
        {
            int iVal;
            if (bVal)
            {
                iVal = 1;
            } 
            else
            {
                iVal = 0;
            }
            

            WritePrivateProfileString(section, key, iVal.ToString(), m_strIniPath);
        }

        public void WriteDouble(string section, string key, double dVal)
        {
            WritePrivateProfileString(section, key, dVal.ToString("F"), m_strIniPath);
        }

        /// <summary> 
        /// 查询ini文件是否存在 
        /// </summary> 
        /// <returns>是否存在</returns> 
        public bool existINIFile()
        {
            return File.Exists(m_strIniPath);
        } 

    }
}
